﻿Imports System.ServiceModel
Imports Ecodex.RestAPI

Public Class Utilerias
    Public Const Integrator_ID As String = "2b3a8764-d586-4543-9b7e-82834443f219"
    Public Const RFC As String = "AAA010101AAA"
    Public Const AltaIntegrador_ID As String = "DF627BC3-A872-4806-BF37-DBD040CBAC7C"



    Public Function CreateToken(ByVal RFC As String, ByVal transactionID As Int64, ByRef token As String) As String
        Dim result As String = String.Empty
        Try
            '#Region "Build Token"
            Using securitySrv = New SrvSecurity.SeguridadClient()
                Dim serviceToken = securitySrv.ObtenerToken(RFC, transactionID)
                Dim toHash = [String].Format("{0}|{1}", Integrator_ID, serviceToken)
                token = Security.Hash(toHash)
                '#End Region
            End Using
            '#Region "Security service exceptions"
        Catch serviceFault As FaultException(Of SrvSecurity.FallaServicio)
            result = serviceFault.Message
        Catch sessionFauld As FaultException(Of SrvSecurity.FallaSesion)
            result = TranslateFault.ToSessionError(sessionFauld.Detail).Description
            '#End Region
        Catch ex As Exception

            result = ex.Message
        End Try
        Return result
    End Function

    Public Function CreateTokenAlta(ByVal RFC As String, ByVal transactionID As Int64, ByRef token As String) As String
        Dim result As String = String.Empty
        Try
            '#Region "Build Token"
            Using securitySrv = New SrvSecurity.SeguridadClient()
                Dim serviceToken = securitySrv.ObtenerToken(RFC, transactionID)
                Dim toHash = [String].Format("{0}|{1}|{2}", Integrator_ID, AltaIntegrador_ID, serviceToken)
                token = Security.Hash(toHash)
                '#End Region
            End Using
            '#Region "Security service exceptions"
        Catch serviceFault As FaultException(Of SrvSecurity.FallaServicio)
            result = serviceFault.Message
        Catch sessionFauld As FaultException(Of SrvSecurity.FallaSesion)
            result = TranslateFault.ToSessionError(sessionFauld.Detail).Description
            '#End Region
        Catch ex As Exception

            result = ex.Message
        End Try
        Return result
    End Function


End Class
